function scrollX() {return window.pageXOffset ? window.pageXOffset : document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft;}

function scrollY() {return window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;}

function SlowScrollTo(left, top)
{
    var y = scrollY();
    var x = scrollX();
    while (x > left || y > top)
    {
        if (x > left)
        {
            x--;
        }
        if (y > top)
        {
            y--;
        }
        scrollTo(x, y);
    }
}

function closeVoisEditorPageTop()
{
    var node;
    node = document.getElementById("awwwe-browser-vois-wizard-form-open-top-button");
    node.style.visibility = "visible";
    node = document.getElementById("awwwe-browser-vois-wizard-form-close-top-button");
    node.style.visibility = "hidden";
    node = document.getElementById("awwwebrowsersite-header");
    node.style.display = "none";
    SlowScrollTo(0,0);
}

function openVoisEditorPageTop()
{
    var node;
    node = document.getElementById("awwwe-browser-vois-wizard-form-open-top-button");
    node.style.visibility = "hidden";
    node = document.getElementById("awwwe-browser-vois-wizard-form-close-top-button");
    node.style.visibility = "visible";
    node = document.getElementById("awwwebrowsersite-header");
    node.style.display = "block";
}

function VOISEditorOnLoad()
{
	initTabPageNodePointers();
    VOISXPath.Initialize();
    VOISEditor.OnLoad();
    SlowScrollTo(0, 0);
    // closeVoisEditorPageTop();
    // showSitePage();
}

function VOISEditorConfirmRebuild()
{
    return confirm(
        "If you want to reset the form to the vois.xml file edits that you had when you last loaded this web page then click \"OK\"."
        + "\r\n"
        + "Clicking \"OK\" will remove any changes that you made after you loaded this web page."
    );
}

function VOISWizardFormOnSubmit()
{
    var nextNode, hostNode;
    nextNode = document.getElementById('awwwe-browser-vois-wizard-form-next_url');
    if(nextNode.value.length > 0)
    {
        hostNode = document.getElementById("awwwe-browser-vois-wizard-form-host");
        if (hostNode != null)
        {
            if (nextNode.value.indexOf(hostNode.value) >= 0)
            {
                // next url has same host
                // make sure that the form has been saved.
                VOISXml.DisplayXml();
                if (VOISXml.DisplayXmlError == true)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                alert("The url you entered in 'Next Web Page URL.' appears to be located at a different web site.");
                return false;
            }
        }
    }
    else
    {
        alert("Please enter 'Next Web Page URL.'");
        return false;  
    }
}

var VOISWizardFormXMLHTTP;

function VOISWizardFormSaveMarkup()
{
    var node;
    var homeurl;
    var markup;
    var params;
    if (VOISWizardFormXMLHTTP != undefined)
    {
    	// still waiting for a response from the server.
    	return
    }
    // make sure that the form has been saved.
    VOISXml.DisplayXml();
    node = document.getElementById(VOISXml.formXmlOutputId);
    if (node != null)
    {
    	markup = node.value.trim()
        if (markup.length == 0)
        {
            alert("You have not defined any visual spaces so there is no VOIS Markup.")
            return
        }
    }
    else
    {
    	alert("Unable to get the VOIS Markup from the editor.")
        return
    }
    
    // send the data
	VOISWizardFormXMLHTTP = new XMLHttpRequest();
    VOISWizardFormXMLHTTP.onreadystatechange=VOISWizardFormSaveMarkupHandler
    VOISWizardFormXMLHTTP.open("POST", VOISXMLSaveURL, true);
	VOISWizardFormXMLHTTP.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    params = ParamVoisMarkup.concat("=", escape(markup),
    	"&", ParamResponsdWithText, "=1");
    //alert(params)
    VOISWizardFormXMLHTTP.send(params)
}

function VOISWizardFormSaveMarkupHandler()
{
	var text;
    if (VOISWizardFormXMLHTTP.readyState==4 && VOISWizardFormXMLHTTP.status==200)
    {
    	text = VOISWizardFormXMLHTTP.responseText
        VOISWizardFormXMLHTTP = undefined
        alert(text);
    }
}

function showSitePageTitle(title)
{
    var node = document.getElementById("awwwe-browser-vois-wizard-form-form-tab-page-title");
    node.innerHTML = title;
}

var webpageTabPagePointer;
var sayasTabPagePointer;
var voisxmlTabPagePointer;
var formTabPagePointer;
var siteTabPagePointer;

var siteTabPointer;
var sayasTabPointer;
var formTabPointer;
var voisxmlTabPointer;
var webpageTabPointer;


function initTabPageNodePointers() {
	// the first page - the web site page    
    siteTabPagePointer = document.getElementById("awwwe-browser-vois-wizard-form-site-tab-page");
    siteTabPointer = document.getElementById("awwwe-browser-vois-wizard-form-tab-web-site");
    // sayas page
    sayasTabPagePointer = document.getElementById("awwwe-browser-vois-wizard-form-sayas-tab-page");
    sayasTabPointer = document.getElementById("awwwe-browser-vois-wizard-form-tab-sayas");
    // visual spaces page
    formTabPagePointer = document.getElementById("awwwe-browser-vois-wizard-form-form-tab-page");
    formTabPointer = document.getElementById("awwwe-browser-vois-wizard-form-tab-visual-spaces");
    // the web page click page
    webpageTabPagePointer = document.getElementById("awwwe-browser-vois-wizard-form-webpage-tab-page");
    webpageTabPointer = document.getElementById("awwwe-browser-vois-wizard-form-tab-web-page");
    // vois markup page
    voisxmlTabPagePointer = document.getElementById("awwwe-browser-vois-wizard-form-voisxml-tab-page");
    voisxmlTabPointer = document.getElementById("awwwe-browser-vois-wizard-form-tab-vois-markup");
}

function showSitePage()
{
    formTabPagePointer.style.display = "none";
    sayasTabPagePointer.style.display = "none";
    webpageTabPagePointer.style.display = "none";
    voisxmlTabPagePointer.style.display = "none";
    siteTabPagePointer.style.display = "block";
    showSitePageTitle("Web Site.");
    siteTabPointer.setAttribute("class", "currentformpagetab");
    sayasTabPointer.setAttribute("class", "notcurrentformpagetab");
    formTabPointer.setAttribute("class", "notcurrentformpagetab");
    voisxmlTabPointer.setAttribute("class", "notcurrentformpagetab");
    webpageTabPointer.setAttribute("class", "notcurrentformpagetab");
}

function showFormPageTitle(title)
{
    var node = document.getElementById("awwwe-browser-vois-wizard-form-form-tab-page-title");
    node.innerHTML = title;
}

function showFormPage()
{
    siteTabPagePointer.style.display = "none";
    sayasTabPagePointer.style.display = "none";
    webpageTabPagePointer.style.display = "none";
    voisxmlTabPagePointer.style.display = "none";
    formTabPagePointer.style.display = "block";
    showFormPageTitle("Visual Spaces.");
    siteTabPointer.setAttribute("class", "notcurrentformpagetab");
    sayasTabPointer.setAttribute("class", "notcurrentformpagetab");
    formTabPointer.setAttribute("class", "currentformpagetab");
    voisxmlTabPointer.setAttribute("class", "notcurrentformpagetab");
    webpageTabPointer.setAttribute("class", "notcurrentformpagetab");
}

function showSayasPage()
{
    siteTabPagePointer.style.display = "none";
    formTabPagePointer.style.display = "none";
    webpageTabPagePointer.style.display = "none";
    voisxmlTabPagePointer.style.display = "none";
    sayasTabPagePointer.style.display = "block";
    showFormPageTitle("Say As.");
    siteTabPointer.setAttribute("class", "notcurrentformpagetab");
    sayasTabPointer.setAttribute("class", "currentformpagetab");
    formTabPointer.setAttribute("class", "notcurrentformpagetab");
    voisxmlTabPointer.setAttribute("class", "notcurrentformpagetab");
    webpageTabPointer.setAttribute("class", "notcurrentformpagetab");
}

function showWebPage()
{
    siteTabPagePointer.style.display = "none";
    formTabPagePointer.style.display = "none";
    sayasTabPagePointer.style.display = "none";
    voisxmlTabPagePointer.style.display = "none";
    webpageTabPagePointer.style.display = "block";
    showFormPageTitle("Web Page.");
    siteTabPointer.setAttribute("class", "notcurrentformpagetab");
    sayasTabPointer.setAttribute("class", "notcurrentformpagetab");
    formTabPointer.setAttribute("class", "notcurrentformpagetab");
    voisxmlTabPointer.setAttribute("class", "notcurrentformpagetab");
    webpageTabPointer.setAttribute("class", "currentformpagetab");
}

function expandWebPageWidth(button)
{
    var node = document.getElementById("awwwe-browser-vois-wizard-web-page-wrapper-div-workarea");
    node.parentNode.style.width = "200%";
    node.style.width = "90%";
    button.setAttribute("onclick", "shrinkWebPageWidth(this);");
    button.value = "Shrink width.";
}

function shrinkWebPageWidth(button)
{
    var node = document.getElementById("awwwe-browser-vois-wizard-web-page-wrapper-div-workarea");
    node.parentNode.style.width = "100%";
    node.style.width = "90%";
    button.setAttribute("onclick", "expandWebPageWidth(this);");
    button.value = "Expand width.";
}

function expandWebPageHeight(button)
{
    var node = document.getElementById("awwwe-browser-vois-wizard-web-page-wrapper-div-workarea");
    node.parentNode.style.height = "100%";
    node.style.height = "100%";
    node.style.overflow = "hidden";
    button.setAttribute("onclick", "shrinkWebPageHeight(this);");
    button.value = "Shrink height.";
}

function shrinkWebPageHeight(button)
{
    var node = document.getElementById("awwwe-browser-vois-wizard-web-page-wrapper-div-workarea");
    node.parentNode.style.height = "100%";
    node.style.height = "600px";
    node.style.overflow = "auto";
    button.setAttribute("onclick", "expandWebPageHeight(this);");
    button.value = "Expand height.";
}

function showVoisXMLPage()
{
    // move the vois xml from the form to the output for the user to copy.
    VOISXml.DisplayXmlToUser();
    // turn the divs on and off.
    siteTabPagePointer.style.display = "none";
    formTabPagePointer.style.display = "none";
    sayasTabPagePointer.style.display = "none";
    webpageTabPagePointer.style.display = "none";
    voisxmlTabPagePointer.style.display = "block";
    showFormPageTitle("VOIS Markup.");
    siteTabPointer.setAttribute("class", "notcurrentformpagetab");
    sayasTabPointer.setAttribute("class", "notcurrentformpagetab");
    formTabPointer.setAttribute("class", "notcurrentformpagetab");
    voisxmlTabPointer.setAttribute("class", "currentformpagetab");
    webpageTabPointer.setAttribute("class", "notcurrentformpagetab");
}
